﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Documents;
using System.IO;
using System.Windows;
using System.Xml.Linq;
using System.Windows.Media;
using System.Windows.Controls;


namespace OneWord
{
    class FileHandle
    {
        private XDocument xDoc;
        private String xFilePath;



        public FileHandle()
        {

        }

        public void LoadExcelFile(String filePath)
        {

        }

        public Boolean LoadTextFile(FlowDocument flowDocument, String filePath)
        {
            TextRange documentTextRange = new TextRange(
                 flowDocument.ContentStart, flowDocument.ContentEnd);
            using (FileStream fileStream = File.Open(filePath, FileMode.Open))
            {
                documentTextRange.Load(fileStream, DataFormats.Text);

                if (flowDocument != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public Boolean LoadXmlFile(String filePath)
        {
            if (File.Exists(filePath))
            {
                this.xFilePath = filePath;
                xDoc = XDocument.Load(filePath);
                return true;
            }
            else
            {
                this.xFilePath = filePath;
                xDoc = new XDocument();
                XElement xWordbook = new XElement("wordbook");
                xDoc.Add(xWordbook);
                xDoc.Save(filePath);

                return false;
            }

        }

        internal IEnumerable<XElement> GetWords()
        {
            var iElements = from element in xDoc.Descendants("item")
                            select element;

            return iElements;
        }


        public void InsertWord(String word, String trans, int year, int index)
        {
            bool isExist = false;

            XElement xxItem = new XElement("item");

            XElement xxxWord = new XElement("word", word);
            XElement xxxTrans = new XElement("trans", trans);
            XElement xxxPhonetic = new XElement("phonetic");
            XElement xxxTags = new XElement("tags");
            XElement xxxProgress = new XElement("progress", "1");

            xxItem.Add(xxxWord, xxxTrans, xxxPhonetic, xxxTags, xxxProgress);

            var tempE = xDoc.Descendants("item");

            foreach (XElement item in tempE)
            {
                if (item.Element("word").Value.Equals(word))
                {
                    if (MessageBox.Show(word + "\n" + trans + "\n is exist,Do you want to add a new one?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                    {
                        break;
                    }
                    else
                    {
                        isExist = true;
                        break;
                    }
                }
            }

            if (!isExist)
            {
                var tempElement = xDoc.Elements("wordbook").ElementAt(0);

                tempElement.Add(xxItem);
                xDoc.Save(xFilePath);
            }

        }

        public void MarkedWord(FlowDocumentPageViewer flowDocumentPageViewer, FlowDocument flowDocument, String word, Color rgbColor)
        {
            //clear previous color 
            Color clearColor = new Color();
            clearColor = Color.FromRgb(255, 255, 255);

            TextRange range = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
            range.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(clearColor));
            // set my color for background

            ChangeColor(rgbColor, flowDocumentPageViewer, flowDocument, word);

        }

        // change background color
        public void ChangeColor(Color l, FlowDocumentPageViewer flowDocumentPageViewer, FlowDocument flowDocument, string keyword)
        {
            //设置文字指针为Document初始位置                      
            TextPointer position = flowDocument.ContentStart;
            //richBox.Document.ContentStart;
            while (position != null)
            {
                //向前搜索,需要内容为Text       
                if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
                {
                    //拿出Run的Text        
                    string text = position.GetTextInRun(LogicalDirection.Forward);
                    //可能包含多个keyword,做遍历查找           
                    int index = 0;
                    index = text.IndexOf(keyword, 0);
                    if (index != -1)
                    {
                        TextPointer start = position.GetPositionAtOffset(index);
                        TextPointer end = start.GetPositionAtOffset(keyword.Length);
                        position = selecta(l, flowDocumentPageViewer, keyword.Length, start, end);
                    }
                }
                //文字指针向前偏移   
                position = position.GetNextContextPosition(LogicalDirection.Forward);

            }
        }
        public TextPointer selecta(Color l, FlowDocumentPageViewer flowDocument, int selectLength, TextPointer tpStart, TextPointer tpEnd)
        {
            TextRange range = flowDocument.Selection;
            range.Select(tpStart, tpEnd);
            //高亮选择         
            range.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(l));
            //range.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);

            return tpEnd.GetNextContextPosition(LogicalDirection.Forward);
        }



    }
}
